package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 订单管理Controller
 *
 * @author silencezheng
 * @date 2022-10-26
 */
@RestController
@RequestMapping("/city")
public class CityController extends BaseController
{
    @Autowired
    private ICityService cityService;

    @RequiresPermissions("system:city:getTree")
    @RequestMapping(value = "/getTree",method = RequestMethod.GET)
    public  AjaxResult selectTree() {
        SysCity structureVo = cityService.selectTree();
        List<SysCity> structureVos = Arrays.asList(structureVo);
       return AjaxResult.success(structureVos);
    }
    /**
     * 获取用户列表
     */
    @RequiresPermissions("system:city:list")
    @GetMapping("/list")
    public TableDataInfo list(SysCity city)
    {
        startPage();
        List<SysCity> list = cityService.selectCityList(city);
        return getDataTable(list);
    }

    /**
     * 新增城市
     */
    @RequiresPermissions("system:city:add")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCity sysCity)
    {
        return toAjax(cityService.insertSysCity(sysCity));
    }

    /**
     * 修改城市
     */
    @RequiresPermissions("system:city:edit")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCity sysCity)
    {
        return toAjax(cityService.updateSysCity(sysCity));
    }

    /**
     * 删除城市
     */
    @RequiresPermissions("system:city:remove")
    @Log(title = "城市", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(cityService.deleteSysCityById(id));
    }
}
